package com.zlq.controller;

import com.zlq.dao.LoginInfoDao;
import com.zlq.myannotation.LoginLog;
import com.zlq.pojo.LoginInfo;
import com.zlq.pojo.User;
import com.zlq.service.UserService;
import com.zlq.utils.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @Autowired
    private LoginInfoDao loginInfoDao;

    @RequestMapping(value = "/login")
    public String login(){
        return "login";
    }


    @RequestMapping(value = "/success")
    public String mainz(@RequestParam("userName") String userName, @RequestParam("password") String passWord){

        User user = new User();
        user.setUsername(userName);
        user.setPassword(passWord);
        System.out.println(userName);
        System.out.println(passWord);
        System.out.println(user.toString());

//        LoginInfo a = new LoginInfo();
//        a.setLogintime("ccc");
//        a.setUsername("aaa");
//        a.setIp("bbb");
//        LoginInfo b = new LoginInfo();
//        b.setLogintime("ccc1");
//        b.setUsername("aaa1");
//        b.setIp("bbb1");

        ToolUtil.getSession().setAttribute("username",userName);

        if (userService.login(user)) {
//            List<LoginInfo> list = {a,b};
            return "success";
        }
        return  "login";
    }
//    @RequestMapping(value = "/upload")
//    public String upload(){
//        return "upload";
//    }
    @RequestMapping(value = "/loginlog")
    public String loginlog(){

        List<LoginInfo> allLog = loginInfoDao.findAllLog();

        ToolUtil.getSession().setAttribute("list",allLog);

        return "loginlog";
    }

}
